package com.tianya.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianya.domain.dto.ParentRequestDTO;
import com.tianya.domain.entity.Clazz;
import com.tianya.domain.entity.Parent;
import com.tianya.domain.entity.Student;
import com.tianya.domain.entity.User;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.domain.vo.ParentVO;
import com.tianya.service.ClazzService;
import com.tianya.service.ParentService;
import com.tianya.mapper.ParentMapper;
import com.tianya.service.StudentService;
import com.tianya.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.awt.event.PaintEvent;

/**
* @author 86191
* @description 针对表【parent(家长表)】的数据库操作Service实现
* @createDate 2025-05-24 18:59:54
*/
@Service
public class ParentServiceImpl extends ServiceImpl<ParentMapper, Parent>
    implements ParentService{
    @Resource
    private UserService userService;


    @Resource
    @Lazy
    private StudentService studentService;

    @Resource
    private ClazzService clazzService;

    @Override
    public void createParent(ParentRequestDTO parentRequestDTO) {
        Parent parent = Parent.builder()
                .studentId(parentRequestDTO.getStudentId())
                .address(parentRequestDTO.getAddress())
                .build();
        //todo 还需要向用户表新增家长角色
        this.save(parent);
    }

    @Override
    public PageResultVO<Parent> pageList(Integer pageNum, Integer pageSize, ParentRequestDTO parentRequestDTO) {
        Page<Parent> pageParam = new Page<>(pageNum, pageSize);
        QueryWrapper<Parent> queryWrapper = new QueryWrapper<>();
        //todo 还需要查询姓名这些返回
        if (parentRequestDTO.getStudentId() != null) {
            queryWrapper.eq("student_id", parentRequestDTO.getStudentId());
        }
        Page<Parent> parentPage = page(pageParam, queryWrapper);
        return PageResultVO.<Parent>builder()
                .total(parentPage.getTotal())
                .records(parentPage.getRecords())
                .build();
    }

    @Override
    public ParentVO getParentById(Long id) {
        User user = userService.getById(id);
        Parent parent = getOne(new QueryWrapper<Parent>()
                .eq("user_id", id));

        ParentVO parentVO = new ParentVO();
        BeanUtil.copyProperties(parent, parentVO);
        parentVO.setName(user!=null?user.getName():"unKnown");
        parentVO.setPhone(user!=null?user.getPhone():"unKnown");
        parentVO.setGender(user!=null?user.getGender():null);

        Student student = studentService.getById(parent!=null?parent.getStudentId():null);
        parentVO.setStudentName(student!=null?student.getName():"unKnown");
        Clazz clazz = student!=null?clazzService.getById(student.getClazzId()):null;
        parentVO.setClazzName(clazz!=null?clazz.getName():"unKnown");
        parentVO.setClazzId(clazz!=null?clazz.getClazzId():null);
        return parentVO;
    }
}




